import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {AppUrl, TIME_OUT} from "../../../../app/app.general";
import {QrCodePage} from "../../../pages";
import {TableQrService} from "./table-qr.service";
import {AppConfig} from "../../../../app/appConfig";
import {CommonService} from "../../../../app/common.service";

/**
 * created by AileYoung on 2018/3/6
 * 餐桌二维码生成
 */

@IonicPage()
@Component({
  selector: 'page-table-qr',
  templateUrl: 'table-qr.html',
})
export class TableQrPage {
  private tableIndex: any;
  private tables = [];
  private getAllTableCodeUrl = '#';

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private httpService: TableQrService,
              private appConfig: AppConfig,
              private cs: CommonService) {
  }

  ionViewDidLoad() {
    this.appConfig.presentLoading('正在获取本店桌台信息', TIME_OUT);
    let uId: any;
    this.httpService.getRid().timeout(TIME_OUT).subscribe(data => {
        let temp: any = data;
        if (temp.code == 200) {
          uId = temp.data;
          this.getAllTableCodeUrl = AppUrl.image.getAllTableCode + "/" + uId;
          this.httpService.getTables().timeout(TIME_OUT).subscribe(data => {
            let temp: any = data;
            this.appConfig.loader.dismiss();
            if (temp.code == 200) {
              this.tables = temp.data;
            }
            else
              this.appConfig.presentToast(temp.msg);
          }, e => {
            this.cs.httpError(e);
            this.appConfig.loader.dismiss();
          })
        }
      }, e => {
        this.cs.httpError(e);
        this.appConfig.loader.dismiss();
      }
    )
    ;

  }

  getTableQrCode() {
    if (this.tableIndex == '' || this.tableIndex == undefined) {
      this.appConfig.presentToast('请先选择桌号');
    }
    else {
      let imageAddress = AppUrl.image.getTableQrCode;
      let params = ['id=' + this.tables[this.tableIndex].rId, 'tableId=' + this.tables[this.tableIndex].tableId];
      this.navCtrl.push(QrCodePage, {imageAddress: imageAddress, params: params, table: this.tables[this.tableIndex]});
    }
  }
}
